% this function computes the moving average of 'det' with a time window of
% w seconds

function detma = ma(det,w,Q)
% Q=1: time window of floor(w/2) before and after.
% Q~=1: time window of floor(w/2) just before.

if (w/2)==floor(w/2);
    error 'w has to be odd'   
end;

for i=1:length(det);
    p(i,1) = max(det(i,1),0);
end;

%detn = no_neg(det);
detn=p;
%detma = smooth(detn,w);
len =length(detn);

if Q==1;
    for i=floor(w/2)+1:len-floor(w/2);
        detma(i)=mean(detn(i-floor(w/2):i+floor(w/2)));
    end;
    for i=1:floor(w/2); 
        detma(i)=mean(detn(1:floor(w/2)+i));    
        dif = len-floor(w/2);
        detma(dif+i)=mean(detn(dif-floor(w/2)+i:len));
    end;
else
    for i=1:floor(w/2);
        detma(i)=mean(detn(1:i));    
    end;
    for i=floor(w/2)+1:len;
        detma(i)=mean(detn(i-floor(w/2):i));
    end;
end;


